package com.fengxu.gamescript.ui.float

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.BaseAdapter
import android.widget.FrameLayout
import android.widget.ListView
import android.widget.TextView
import com.fengxu.gamescript.R
import com.fengxu.gamescript.data.KeyConf
import com.fengxu.gamescript.util.CommonUtils
import com.fengxu.gamescript.util.FloatUtils
import com.xuexiang.xui.utils.DensityUtils
import org.jetbrains.anko.backgroundColor
import org.jetbrains.anko.padding
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.singleLine
import org.jetbrains.anko.textColor

/**
 * 键位配置快捷选择栏
 */
class FloatKeyConfQuickSelectView(context: Context) : FrameLayout(context) {

    // 键位配置的列表组件
    private val keyConfListView: ListView by lazy {
        findViewById(R.id.flow_quickSelectKey_list)
    }

    // 用户已经设置的键位配置
    private val keyConf:KeyConf by lazy { KeyConf.readKeyConf(context) }

    // 是否显示了
    var isShow:Boolean = false
        private set

    init {
        LayoutInflater.from(context).inflate(R.layout.flow_quick_select_keyconf,this)
        backgroundColor = Color.parseColor("#33000000")
        // 点击其他地方关闭悬浮窗
        onClick {
            close()
        }
        initView()
    }

    // 初始化列表中的配置项
    private fun initView() {
        // 设置适配器
        keyConfListView.padding = DensityUtils.dp2px(10F)
        keyConfListView.adapter = object: BaseAdapter(){
            override fun getCount(): Int {
                return keyConf.keyConfDataList.size
            }

            override fun getItem(position: Int): Any {
                return position
            }

            override fun getItemId(position: Int): Long {
                return position.toLong()
            }

            override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
                // 不采用缓存
                return TextView(context).apply {
                    textColor = Color.BLUE
                    singleLine = true
                    ellipsize = TextUtils.TruncateAt.END
                    text = keyConf.keyConfDataList[position].name
                    textSize = DensityUtils.sp2px(5F).toFloat()
                    padding = DensityUtils.dp2px(10F)
                    val bg = GradientDrawable()
                    if(keyConf.keyConfDataList[position].name == keyConf.activeKeyConfData.name){
                        bg.setStroke(DensityUtils.dp2px(1F),Color.BLUE)
                    }else{
                        bg.setStroke(DensityUtils.dp2px(1F),Color.TRANSPARENT)
                    }
                    background = bg
                    onClick {
                        if(position < keyConf.keyConfDataList.size){
                            keyConf.activeConf(keyConf.keyConfDataList[position].name)
                            KeyConf.saveConf(context)
                            CommonUtils.showDesktopToast(context,"应用方案成功")
                        }else{
                            CommonUtils.showDesktopToast(context,"应用方案失败")
                        }
                        close()
                    }
                }
            }

        }
    }

    /**
     * 显示悬浮窗
     */
    fun showFloat(){
        val windowManager = FloatUtils.getWindowManager(context)
        val lp = FloatUtils.getDefaultFloatParams()
        lp.flags = lp.flags or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        val screenSize = DensityUtils.getScreenMetrics(true)
        lp.width = screenSize.widthPixels
        lp.height = screenSize.heightPixels;
        windowManager.addView(this,lp)
        // 设置空白处
        val pdHor = DensityUtils.dp2px(40F)
        val pdTop = screenSize.heightPixels / 6
        setPadding(pdHor,pdTop,pdHor,pdTop)
        isShow = true
    }

    /**
     * 关闭窗体
     */
    fun close(){
        if(isShow){
            FloatUtils.getWindowManager(context).removeView(this@FloatKeyConfQuickSelectView)
        }
    }


}